<?php

namespace app\api\controller;

use Throwable;
use app\common\controller\Frontend;
use think\facade\Db;
use app\track\model\GetTrack as GetTrackModel;
use app\api\logic\TrackLogic;
use app\data\util\HttpUtil;

class Track extends Frontend
{
    protected array $noNeedLogin = ['*'];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
    }

    //注册17track
    public function sync_track_register()
    {
        $items = Db::name('gettrackinfo')
            ->where('is_sync', 3)
            ->limit(1)
            ->order('id desc')
            ->select()
            ->toArray();
        if (!empty($items)) {
            foreach ($items as $item) {
                $this->web_register($item);
                $sheetOrderArr[] = [
                    'id' => $item['id'],
                    'is_sync' => 0,
                ];
            }
            (new GetTrackModel())->saveAll($sheetOrderArr);

            $this->success('已更新');
        } else {
            $this->success('已全部更新');
        }
    }

    public function web_register($order)
    {
        $data = array();
        $data['number'] = $order['number'];
        $data2[] = $data;
        $url = "https://api.17track.net/track/v2.2/register";
        HttpUtil::trackPostUrl($url, $data2);
        return true;
    }

    /**
     * 获取17track详情
     * @throws Throwable
     */
    public function gettrackinfo()
    {
        $items = Db::name('gettrackinfo')
            ->where('is_sync', 0)
            ->limit(10)
            ->select()
            ->toArray();
        if (!empty($items)) {
            $orders = array();
            foreach ($items as $item) {
                $data = array();
                $data['number'] = $item['number'];
                $orders[] = $data;
            }
            $this->gettrack($orders);
            $this->success('已更新');
        } else {
            $this->success('已全部更新');
        }
    }

    public function gettrack($orders)
    {
        $url = "https://api.17track.net/track/v2.2/gettrackinfo";
        $return = HttpUtil::trackPostUrl($url, $orders);
        // dump($return0);die;
        // $return = json_decode($return0, true);
        if(!empty($return['data']['accepted']) && isset($return['data']['accepted'])){
            //批量处理
            $this->track_info($return['data']['accepted']);
        }
        if(!empty($return['data']['rejected']) && isset($return['data']['rejected'])){
             //批量处理
            $this->track_info($return['data']['rejected']);
        }
        return true;
    }

    /**
    * 批量处理
    * @param array params
    */
    public function track_info($data)
    {
        foreach ($data as $k=>$v) {
            $order_arr = array();
            $order_arr['time'] = time();
            if(!empty($v['track_info']['latest_status']['status']) && isset($v['track_info']['latest_status']['status'])){
                $track_info = $v['track_info'];
                $order_arr['status'] = $track_info['latest_status']['status'];
                $order_arr['is_sync'] = 1;
                 //新增字段
                if(!empty($track_info['latest_event']['time_iso']) && isset($track_info['latest_event']['time_iso'])){
                    $order_arr['time_iso'] = $track_info['latest_event']['time_iso'];
                }
                if(!empty($track_info['latest_event']['description']) && isset($track_info['latest_event']['description'])){
                    $order_arr['description'] = $track_info['latest_event']['description'];
                }
                if(!empty($track_info['latest_event']['location']) && isset($track_info['latest_event']['location'])){
                    $order_arr['location'] = $track_info['latest_event']['location'];
                }
            }else{
                $order_arr['is_sync'] = 2;
            }
            Db::name('gettrackinfo')
                ->where('number',$v['number'])
                ->update($order_arr);
        }
        return true;
    }

    /**
    * 无忧对账对账
    * @param array params
    */
    public function reconciliation_qqwy($id = null)
    {
        $reconciliation_qqwy = (new TrackLogic())->reconciliation_qqwy($id);
        if($reconciliation_qqwy){
            $this->success('操作成功',$reconciliation_qqwy);
        }else{
            $this->success('已全部更新');
        }
    }
    
    /**
    * 旧金山对账对账
    * @param array params
    */
    public function reconciliation_jjs($id = null)
    {
        $reconciliation_qqwy = (new TrackLogic())->reconciliation_jjs($id);
        if($reconciliation_qqwy){
            $this->success('操作成功',$reconciliation_qqwy);
        }else{
            $this->success('已全部更新');
        }
    }

    /**
    * SKU数量以及订单运费对账
    * @param array params
    */
    public function reconciliation_order($id = null)
    {
        $reconciliation_order = (new TrackLogic())->reconciliation_order($id);
        if($reconciliation_order){
            $this->success('操作成功',$reconciliation_order);
        }else{
            $this->success('已全部更新');
        }
    }

    /**
    * 顺丰对账
    * @param array params
    */
    public function reconciliation_sf($id = null)
    {
        $reconciliation_sf = (new TrackLogic())->reconciliation_sf($id);
        if($reconciliation_sf){
            $this->success('操作成功',$reconciliation_sf);
        }else{
            $this->success('已全部更新');
        }
    }
    /**
    * 提前算sku
    * @param array params
    */
    public function CalculateSKUInAdvance($id = null)
    {
        $CalculateSKUInAdvance = (new TrackLogic())->CalculateSKUInAdvance($id);
        if($CalculateSKUInAdvance){
            $this->success('操作成功',$CalculateSKUInAdvance);
        }else{
            $this->success('已全部更新');
        }
    }
}